﻿Chenxin.UI.OrganizationMgr = Chenxin.extend(Chenxin.UI.PageCommon, {
    actionUrl: 'service/OrganizationMgr.ashx',
    afterGetModel: function(model) {
        this.addType = model.DepartProperty;

    },
    afterDelSucc: function(data) {
        if (!data.isDelUser) {
            this.tree.reload(this.tree.findParentNode(this.tree.currentNode));

            delete this.currentId;
            delete this.tree.currentNode;
        } else {
            this.empGrid.reload({ depid: this.tree.currentNode.DepartId })
        }
    },
    initEvent: function() {
        var _this = this;
        $('.icoNewsView').click(function() {
            switch (this.name) {
                case 'addcity': //添加城市
                    //_this.tree.reload(_this.tree.root);
                    _this.addDepart(100);
                    break;
                case 'addcompany': //添加公司
                    _this.addDepart(200);
                    break;
                case 'adddeparts': //添加部门
                    _this.addDepart(500);
                    break;
                case 'addbusiness': //添加商圈
                    _this.addDepart(300);
                    break;
                case 'addsort': //添加分类
                    _this.addDepart(400);
                    break;
                case 'addstore': //添加门店
                    _this.addDepart(500);
                    break;
                case 'addotherdeparts': //添加其他部门
                    _this.addDepart(600);
                    break;
                case 'editnode': //编辑节点
                    _this.editDepart();
                    break;
                case 'movenode': //转移节点
                    _this.showMoveNodeWin();
                    break;
                case 'delnode': //删除节点
                    _this.delDepart();
                    break;
                case 'empsetting': //人员设定
                    _this.showEmpSetting();
                    break;
                case 'changepassword': //密码修改
                    _this.showChangePwd();
                    break;
                case 'businessbelongto': //业务所属地
                    _this.showBusinessSetting();
                    break;
                case 'defauthority': //默认职权设定
                    _this.showDefAuthoritySetting();
                    break;
                case 'del':
                    _this.delEmp();
                    break;
                case 'exportdata': //导出Excel
                    _this.empGrid.exportData();
                    break;
            }
        });



        $('.tr_houselist').live('click', function() {
            $('.tr_houselist').each(function() {
                $(this).removeClass('selected_orgbgcolor');
            });

            $(this).toggleClass('selected_orgbgcolor');
            _this.currentUserId = this.id;
        });
        $('.tr_houselist').live('mouseover', function() {
            $(this).toggleClass('orgbgcolor');
        });
        $('.tr_houselist').live('mouseout', function() {
            $(this).toggleClass('orgbgcolor');
        });
    },
    delEmp: function() {
        if ($('#' + this.currentUserId + ' td:eq(2)').text()) {
            alert('隶属员工不能删除！');
            return;
        }
        this.del('deluser', '', this.actionUrl, { userid: this.currentUserId }, this.currentUserId);
    },
    showMoveNodeWin: function() {
    if (!this.tree.currentNode) {
        
            alert('请选择一个“部门”或“其他部门”的组织结构节点！');
            return;
        }
        if (this.tree.currentNode.DepartProperty == 500 || this.tree.currentNode.DepartProperty == 600) {
            var title = '';
            this.currentId = this.tree.currentNode.DepartId;

            if (!this.moveNodeWin) {
                this.moveNodeWin = new Chenxin.UI.MoveNodeForm({
                    scope: this,
                    afterGetFormJson: function(model) {
                        this.moveNodeWin.additionalParam.parentid = model.parentid;
                        this.moveNodeWin.additionalParam.depid = this.tree.currentNode.DepartId;
                        this.moveNodeWin.additionalParam.depname = this.tree.currentNode.DepartName;
                        delete model.parentid;
                    },
                    saveSucc: function() {
                        this.tree.reload(this.tree.findParentNode(this.tree.currentNode));
                        this.tree.reload(this.tree.findNodeById(this.moveNodeWin.additionalParam.parentid));
                    }
                });
            }
            this.moveNodeWin.op = 'move';
            if (this.moveNodeWin.show()) {
                this.moveNodeWin.reset();
                this.moveNodeWin.setTitle('转移节点');
            }
        } else {
            alert('该节点不可转移,只可转移“部门”或“其他部门”！');
        }
    },
    showDefAuthoritySetting: function() {
        if (!this.defAuthoritySettingWin) {
            this.defAuthoritySettingWin = new Chenxin.UI.DefAuthoritySetting({
                scope: this,
                afterGetFormJson: function(model) {
                    Chenxin.apply(this.defAuthoritySettingWin.additionalParam, model);
                }
            })
        }
        this.defAuthoritySettingWin.op = 'settermssetting';
        if (this.defAuthoritySettingWin.show()) {
            this.defAuthoritySettingWin.reset();
            this.defAuthoritySettingWin.setTitle('默认职权设定');
        }
    },
    showBusinessSetting: function() {
        if (!this.businessSettingWin) {
            this.businessSettingWin = new Chenxin.UI.BusinessSetting({
                scope: this,
                afterGetFormJson: function(model) {
                    delete model.UserName;
                    Chenxin.apply(this.businessSettingWin.additionalParam, model);
                    this.businessSettingWin.additionalParam.userid = this.businessSettingWin.currentUserId;
                }
            });
        }
        this.businessSettingWin.op = 'local';
        if (this.businessSettingWin.show()) {
            this.businessSettingWin.reset();
            this.businessSettingWin.setTitle('业务所属地');
        }
    },
    showEmpSetting: function() {
        if (!this.empSettingWin) {
            this.empSettingWin = new Chenxin.UI.EmpSettingForm({
                scope: this,
                op: 'empsetting',
                afterGetFormJson: function(model) {
                    if (model.settingtype == '10') {//门店
                        if (model.role) {
                            delete model.role;
                        }
                        if (model.usercode) {
                            delete model.usercode;
                        }
                        Chenxin.apply(this.empSettingWin.additionalParam, model);
                    } else if (model.settingtype == '20') {//个人
                        if (model.depid) {
                            delete model.depid;
                        }


                        // this.empSettingWin.additionalParam.usercode=model.usercode;
                        // this.empSettingWin.additionalParam.userid=model.userid;
                        /*
                        var _usercode=this.empSettingWin.findByName('usercode');
                        if(_usercode.currentNode){			
                        model.usercode=this.empSettingWin.findByName('usercode').currentNode.code;
                        }*/
                        Chenxin.apply(this.empSettingWin.additionalParam, model);
                    } else {
                        // this.empSettingWin.additionalParam.usercode=model.usercode;
                        // this.empSettingWin.additionalParam.userid=model.userid;
                        // this.empSettingWin.additionalParam.aimusercode=model.aimusercode;
                        // this.empSettingWin.additionalParam.aimuserid=model.aimuserid;
                        /*
                        var _usercode=this.empSettingWin.findByName('usercode');
                        if(_usercode.currentNode){		
                        this.empSettingWin.additionalParam.usercode=_usercode.currentNode.code;
                        this.empSettingWin.additionalParam.userid=model.usercode;
                        }
                        var _aimusercode=this.empSettingWin.findByName('aimusercode');
                        if(_aimusercode.currentNode){			
                        this.empSettingWin.additionalParam.aimusercode=_aimusercode.currentNode.code;
                        this.empSettingWin.additionalParam.aimuserid=model.aimusercode;	
                        }		*/
                        Chenxin.apply(this.empSettingWin.additionalParam, model);
                        this.empSettingWin.additionalParam.settingtype = 30;
                    }
                    Chenxin.Fn.initObject(model);
                }
            });
        }
        this.empSettingWin.show();
    },
    showChangePwd: function() {
        if (!this.changePwdWin) {
            this.changePwdWin = new Chenxin.UI.ResetPwdForm({
                scope: this,
                afterGetFormJson: function(model) {
                    delete model.UserCode;
                    Chenxin.apply(this.changePwdWin.additionalParam, model);
                    this.changePwdWin.additionalParam.userid = this.changePwdWin.currentUserId;
                }
            });
        }
        this.changePwdWin.op = 'resetpwd';
        if (this.changePwdWin.show()) {
            this.changePwdWin.reset();
            this.changePwdWin.setTitle('重置密码');
        }
    },
    editDepart: function() {
        if (!this.tree.currentNode) {
            alert('请选择一个非城市，公司的组织结构节点！');
            return;
        }
        if (this.tree.currentNode == this.tree.root) {
            alert('根节点不可编辑！');
            return;
        }
        var title = '';
        switch (parseInt(this.tree.currentNode.DepartProperty)) {
            case 100:
                title = '城市';
                $('#tr-moreitems').hide();
                break;
            case 200:
                title = '公司';
                $('#tr-moreitems').hide();
                break;
            case 300:
                title = '商圈';
                $('#tr-moreitems').hide();
                break;
            case 400:
                title = '分类';
                $('#tr-moreitems').hide();
                break;
            case 500:
                title = '门店';
                this.form.reloadRegionCtr(this.tree.findParentNode(this.tree.currentNode).CityId);
                $('#tr-moreitems').show();
                break;
            case 600:
                title = '其他部门';
                this.form.reloadRegionCtr(this.tree.findParentNode(this.tree.currentNode).CityId);
                $('#tr-moreitems').show();
                break;
        }
        this.currentId = this.tree.currentNode.DepartId;
        this.edit();
        this.form.setTitle('编辑' + title);
        $('#lbl_departname').text(title + '名称');
        $('#lbl_departcode').text(title + '编号');
        this.form.findByName('DepartCode').attr('disabled', 'disabled');
        this.form.findByName('DepartCode').unFormValidator(true);
    },
    delDepart: function() {
        if (!this.tree.currentNode) {
            alert('请选择一个组织结构节点！');
            return;
        }
        this.currentId = this.tree.currentNode.DepartId;
        this.del();
    },
    addDepart: function(type) {
        this.addType = type;
        if (!this.tree.currentNode) {
            alert('请选择一个组织结构节点！');
            return;
        }
        var title = "部门";
        switch (type) {
            case 100:
                if (this.tree.currentNode != this.tree.root) {
                    alert('该节点不能添加城市！');
                    return;
                }
                title = '城市';
                break;
            case 200:
                if (this.tree.currentNode.DepartProperty != 100) {
                    alert('该节点不能添加公司！');
                    return;
                }
                title = '公司';
                break;
            case 300:
                if (this.tree.currentNode.DepartProperty == 200 || this.tree.currentNode.DepartProperty == 400) {
                    title = '商圈';
                    break;
                } else {
                    alert('该节点不能添加商圈！');
                    return;
                }
            case 400:
                if (this.tree.currentNode.DepartProperty != 200) {
                    alert('该节点不能添加分类！');
                    return;
                }
                title = '分类';
                break;
            default:
                if (this.tree.currentNode.DepartProperty == 300 || this.tree.currentNode.DepartProperty == 400) {
                    if (type == 500) {
                        title = '门店';
                    } else {
                        title = '其他部门';
                    }
                } else {
                    if (type == 500) {
                        alert('该节点下不能添加门店');
                    } else {
                        alert('该节点下不能添加其他部门');
                    }
                    return;
                }
                break;

        }        
        
        if (type == 500 || type == 600) {
            $('#tr-moreitems').show();
            
            //加载区域和管理区域
            this.form.reloadRegionCtr(this.tree.findParentNode(this.tree.currentNode).CityId);
        } else {
			if(type==200){				
				$('#tr_areasetting').show();
				$('#tr_areatype').show();
			}else{
				$('#tr_areasetting').hide();
				$('#tr_areatype').hide();			
			}
            $('#tr-moreitems').hide();
        }
        if (type == 100) {
            this.add('addcity');
        } else if (type == 200) {
            this.add('addcompany');
        } else {
            this.add();
        }
        if (type == 500 || type == 600) {
            //初始化值
            this.form.fitData({
                CanAddAtCityId: this.tree.currentNode.CityId,
                ViewOwnerNum: 10,
                LockHouseDay: 15,
                ProtectedNum: 3,
                MarkProtectHours: 36,
                CustomerDemandNum: 0,
                OverTimeTraceNum: 0,
                IsProtectMark: true,
                IsCalculate: true
            });
        } else {	
			if(type==200){
				$('#tr_areasetting').show();
				$('#tr_areatype').show();			
			}else{
				$('#tr_areasetting').hide();
				$('#tr_areatype').hide();
			}
            this.form.reset();
        }

        this.form.setTitle('添加' + title);
        $('#lbl_departname').text(title + '名称');
        $('#lbl_departcode').text(title + '编号');
        this.form.findByName('DepartCode').removeAttr('disabled');
        this.form.findByName('DepartCode').unFormValidator(false);
    },
    createTree: function() {
        var _this = this;
        return new Chenxin.UI.Tree({
            actionUrl: this.actionUrl,
            autoMask: false,
            renderTo: 'tree_departs',
            txtField: 'DepartName',
            valField: 'DepartId',
            title: '组织结构',
            param: { type: 0 },
            checkIsLeaf: function(items) {
                if (items.length) {
                    if (items[0].DepartProperty == '500' || items[0].DepartProperty == '600') {
                        this.setLeaf(items);
                    }
                }
            },
            beforeExpand: function(node) {
                this.param.type = node.DepartProperty;
            },
            beforeReload: function(node) {
                this.param.type = node.DepartProperty;
                _this.empGrid.clear();
                $('#emplist_table').css('min-height', '400px');
            },
            root: {
                DepartId: '00000000-0000-0000-0000-000000000000',
                DepartName: '易居臣信',
                DepartProperty: '0'
            },
            onSelected: function(node, el) {
                if (node.DepartProperty == 500 || node.DepartProperty == 600) {
                    _this.empGrid.reload({ depid: node.DepartId });
                }
            }
        });
    },
    createEmpGrid: function() {
        return new Chenxin.UI.Pagination({
            actionUrl: this.actionUrl + '?op=getlist',
            renderTo: 'emplist_table',
            autoLoad: false,
            minHeight: 400,
            pageSize: 10
        });
    },
    createForm: function() {
        return new Chenxin.UI.OrganizationForm({
            scope: this,
            afterFitData: function(data) {
				if(this.form.op=='edit'){					
					$('#tr_areasetting').hide();
					$('#tr_areatype').hide();
				}
                if (data.DepartProperty == 500 || data.DepartProperty == 600) {
                    Chenxin.Fn.getModel(this.currentId, this.actionUrl, function(model) {
                        model.IsOpen = this.currentModel.IsOpen;
                        this.form.fitData(model, true);
                    }, this, 'getsettingmodel');
                }else if(data.DepartProperty == 200){
					Chenxin.Fn.getModel(this.currentId, this.actionUrl, function(model) {
                        this.form.fitData(model, true);
						$('#tr_areasetting').show();
						if(model.AreaUse==true){
							$('#tr_areatype').show();
						}else{
							$('#tr_areatype').hide();						
						}
                    }, this, 'getareausemodel');
				}
            },
            saveSucc: function() {
                if (this.form.op == 'add' || this.form.op == 'addcity' || this.form.op == 'addcompany') {
                    this.tree.reload(this.tree.currentNode);
                } else if (this.form.op == 'edit') {
                    this.tree.reload(this.tree.findParentNode(this.tree.currentNode));
                    delete this.currentId;
                    delete this.tree.currentNode;
                }
            },
            afterGetFormJson: function(model) {
                var x = this.tree.currentNode;
                if (x.CityId) {
                    model.CityId = x.CityId;
                }
                if (x.City) {
                    model.City = x.City
                }
                if (x.CompanyId) {
                    model.CompanyId = x.CompanyId;
                }
                if (x.Company) {
                    model.Company = x.Company;
                }
                model.DepartProperty = this.addType;

                this.form.additionalParam.fatherid = this.tree.currentNode.DepartId;
                if (this.addType == 500 || this.addType == 600) {
                    var settingModel = {};
                    if (model.LockHouseDay != undefined) {
                        settingModel.LockHouseDay = model.LockHouseDay;
                        delete model.LockHouseDay;
                    }
                    if (model.ProtectedNum != undefined) {
                        settingModel.ProtectedNum = model.ProtectedNum;
                        delete model.ProtectedNum;
                    }
                    if (model.ViewOwnerNum != undefined) {
                        settingModel.ViewOwnerNum = model.ViewOwnerNum;
                        delete model.ViewOwnerNum;
                    }
                    if (model.IsHideHouse != undefined) {
                        settingModel.IsHideHouse = model.IsHideHouse;
                        delete model.IsHideHouse;
                    }
                    if (model.IsAloneUserCode != undefined) {
                        settingModel.IsAloneUserCode = model.IsAloneUserCode;
                        delete model.IsAloneUserCode;
                    }
                    if (model.IsProtectMark != undefined) {
                        settingModel.IsProtectMark = model.IsProtectMark;
                        delete model.IsProtectMark;
                    }
                    if (model.MarkProtectHours != undefined) {
                        settingModel.MarkProtectHours = model.MarkProtectHours;
                        delete model.MarkProtectHours;
                    }
                    if (model.AllowPrivHouse != undefined) {
                        settingModel.AllowPrivHouse = model.AllowPrivHouse;
                        delete model.AllowPrivHouse;
                    }
                    if (model.AllowPubDemand != undefined) {
                        settingModel.AllowPubDemand = model.AllowPubDemand;
                        delete model.AllowPubDemand;
                    }
                    if (model.IsUseBlacklist != undefined) {
                        settingModel.IsUseBlacklist = model.IsUseBlacklist;
                        delete model.IsUseBlacklist;
                    }
                    if (model.CanAddAtCityId != undefined) {
                        settingModel.CanAddAtCityId = model.CanAddAtCityId;
                        delete model.CanAddAtCityId;
                    }
                    if (model.CanAddAtCityName != undefined) {
                        settingModel.CanAddAtCityName = model.CanAddAtCityName;
                        delete model.CanAddAtCityName;
                    }
                    if (model.BlacklistLevel != undefined) {
                        settingModel.BlacklistLevel = model.BlacklistLevel;
                        delete model.BlacklistLevel;
                    }
                    if (model.CustomerDemandNum != undefined) {
                        settingModel.CustomerDemandNum = model.CustomerDemandNum;
                        delete model.CustomerDemandNum;
                    }
                    if (model.OverTimeTraceNum != undefined) {
                        settingModel.OverTimeTraceNum = model.OverTimeTraceNum;
                        delete model.OverTimeTraceNum;
                    }
                    if (model.IsTelRepeat != undefined) {
                        settingModel.IsTelRepeat = model.IsTelRepeat;
                        delete model.IsTelRepeat;
                    }

                    this.form.additionalParam.settingModel = settingModel.toString();
                } else {
					if(this.addType==200){
						this.form.additionalParam.AreaUse=model.AreaUse;
						if(model.AreaUse=='false'){							
							this.form.additionalParam.AreaType=0;
						}else{
							this.form.additionalParam.AreaType=model.AreaType;
						}
					}
                    if (model.BlacklistLevel) {
                        delete model.BlacklistLevel;
                    }
                }
				delete model.AreaUse;
				delete model.AreaType;

				
                if (this.form.op == 'edit') {
					if(!model.IsRoof){
						model.IsRoof='false';
					}
					if(!model.IsCalculate){
						model.IsCalculate='false';
					}
					
					if(!model.IsContractCenter){
						model.IsContractCenter='false';
					}
                    Chenxin.applyIf(model, this.currentModel);
                }
            }
        });
    },
    initComponent: function() {
        Chenxin.UI.OrganizationMgr.superclass.initComponent.call(this);
        this.tree = this.createTree();
        this.empGrid = this.createEmpGrid();
    }
});

Chenxin.UI.OrganizationForm=Chenxin.extend(Chenxin.UI.FormCommon,{
	actionUrl:'service/OrganizationMgr.ashx',
	id:'departWin',
	renderTo:'jqm_depart',
	jqmWidth:700,
	defBBtn:true,
	reloadRegionCtr:function(cityid){
		var region=this.findByName('Region');
		region.param.filter='bycity';
		region.param.cityid=cityid;
		region.reload();
		var regionCtr=this.findByName('RegionCtr');
		regionCtr.param.cityid=cityid;
		regionCtr.param.filter='bycity';
		regionCtr.reload();
	},
	formValidator:function(model){
		if(this.scope.addType==500||this.scope.addType==600){
			return Chenxin.UI.OrganizationForm.superclass.formValidator.call(this,model);
		}else{
			if(!jQuery.formValidator.pageIsValid(this.id)){
				alert('请填写完必填(选)项再进行表单提交！');
				return false;
			}
		}
		return true;
	},
	createComboList:function(){
		var region=new Chenxin.UI.ParameterCombo({
			name:'Region',
			containerId:'#'+this.id+'_Region',
			key:'System.Region',
			inModelType:'txt',
			autoLoad:false,
			required:true
		});
		var regionCtr=new Chenxin.UI.ParameterCombo({
			name:'RegionCtr',
			containerId:'#'+this.id+'_RegionCtr',
			key:'System.Region',
			showMode:'multi',
			colNum:3,
			inModelType:'txt',
			autoLoad:false,
			tbxWidth:250
		});
		
		var departKind=new Chenxin.UI.Combo({
			containerId: '#'+this.id+'_DepartKind',
			name: 'DepartKind',
			inModelType:'txt',
			items:comboCommon.departKind,
			required:true	
		});
		
		var canAddCity=new Chenxin.UI.StructCombo({
			param:{op:'getcity'},
			containerId:'#'+this.id+'_canaddcity',
			name:'CanAddAtCityId',
			inModelType:'all',
			txtModelName:'CanAddAtCityName',
			showMode:'multi',
			colNum:3,
			tbxWidth:200
		});
		return [region,regionCtr,departKind,canAddCity];
	},
	initEvent:function(){
		this.findByName('AreaUse').bind('click',function(){
			if(this.value=="true"){
				$('#tr_areatype').show();
			}else{
				$('#tr_areatype').hide();			
			}
		});
		
	},
	createForm:function(){
		return Chenxin.FormCommon.organizationForm(this.id);
	},
	initFormValidator:function(){
		var _this=this;
		$('#departcode').formValidator({
			validatorgroup:this.id,
            onshow: "请输入编号!", 
			onfocus: "请输入编号!", 
			oncorrect: " "
        }).inputValidator({ //非空验证
            min: 1, 
			empty: { 
				leftempty: false, 
				rightempty: false, 
				emptyerror: "编号不能为空!" 
			}, 
			onerror: "编号不能为空!"
        }).functionValidator({
            fun: function(val, elem) {
                var exist = true;
				Chenxin.ajax({
					type: "get",
					dataType: "json",
					async: false,
					url: _this.actionUrl,
					data: { op: 'checkcode', name: val, fatherid:_this.scope.tree.currentNode.DepartId,type:_this.scope.addType,submitop:_this.op},
					onSuccess: function(data) {
						exist = data.exist;
					}
				});
				if (exist) {
					return '编号已经存在！';
				} else {
					return true;
				}
            },
            onerror: '编号已经存在！'
        });
		
		$('#departname').formValidator({
			validatorgroup:this.id,
            onshow: "请输入编号!", 
			onfocus: "请输入编号!", 
			oncorrect: " "
        }).inputValidator({ //非空验证
            min: 1, 
			empty: { 
				leftempty: false, 
				rightempty: false, 
				emptyerror: "编号不能为空!" 
			}, 
			onerror: "编号不能为空!"
        }).functionValidator({
            fun: function(val, elem) {
                var exist = true;
				Chenxin.ajax({
					type: "get",
					dataType: "json",
					async: false,
					url: _this.actionUrl,
					data: { op: 'checkname', name: val, fatherid:_this.scope.tree.currentNode.DepartId,type:_this.scope.addType,submitop:_this.op},
					onSuccess: function(data) {
						exist = data.exist;
					}
				});
				if (exist) {
					return '编号已经存在！';
				} else {
					return true;
				}
            },
            onerror: '编号已经存在！'
        });;
	},
	initComponent:function(){
		Chenxin.UI.OrganizationForm.superclass.initComponent.call(this);
	}
});

Chenxin.UI.BusinessSetting=Chenxin.extend(Chenxin.UI.FormCommon,{
	actionUrl:'service/OrganizationMgr.ashx',
	id:'businessSettingWin',
	renderTo:'jqm_businessSetting',
	jqmWidth:420,
	createForm:function(){
		return Chenxin.FormCommon.businessSetting(this.id);
	},
	initFormValidator:function(){
		var _this=this;
		$('#'+this.id+'_usercode').formValidator({
			validatorgroup:this.id,
            onshow: "请输入用户名!", 
			onfocus: "请输入用户名!", 
			oncorrect: " "
        }).inputValidator({ //非空验证
            min: 1, 
			empty: { 
				leftempty: false, 
				rightempty: false, 
				emptyerror: "用户名不能为空!" 
			}, 
			onerror: "用户名不能为空!"
        }).functionValidator({
            fun: function(val, elem) {
                var exist = true;
				Chenxin.ajax({
					type: "get",
					dataType: "json",
					async: false,
					url: _this.actionUrl,
					data: { op: 'getuserbycode', usercode: val},
					onSuccess: function(data) {
						if(data.userinfo){
							_this.findByName('UserName').val(data.userinfo);
							_this.currentUserId=data.UserId;
													
						}
						exist=data.exist;
					}
				});
				if (!exist) {
					return '用户名不存在';
				} else {
					return true;
				}
            },
            onerror: '用户名不存在！'
        });
		
	},
	initEvent:function(){	
		var _this=this;
		var filter='#'+this.id;
		$(filter+'_Save').live('click',function(){			
			_this.save();
		});
		$(filter+'_Cancle').live('click',function(){
			_this.hide();			
		});	
		
		
	},
	createComboList:function(){
		//城市
        var city = new Chenxin.UI.StructCombo({
            param: { op: 'getcity' },
            containerId: '#' + this.id + '_City',
            name: 'cityid',
            tbxWidth: 130,
            inModelType: 'all',
			txtModelName:'city',
            onSelected: function() {
                if (this.getValue() == this.defVal) {
                    company.clearItems();
                } else {
                    company.param.fatherid = this.getValue();
                    company.reload();
                }
				business.clearItems();
				depart.clearItems();
            }
        });

        //公司
        var company = new Chenxin.UI.StructCombo({
            autoLoad: false,
            param: { op: 'getcompany' },
            name: 'companyid',
			txtModelName:'company',
			inModelType:'all',
            containerId: '#' + this.id + '_Company',
            tbxWidth: 130,
			onSelected:function(){
				if(this.getValue()==this.defVal){
					business.clearItems();	
				}else{
					business.param.fatherid=this.getValue();
					business.reload();
				}
				depart.clearItems();
			}
        });
		
		var business = new Chenxin.UI.StructCombo({
            param: { op: 'getbusinessdistrict' },
            containerId: '#' + this.id + '_BusinessDistinct',
            tbxWidth: 130,
            colWidth: 120,  
			autoLoad:false,
            inModelType: 'none',
            onSelected: function() {
                if (this.getValue() == this.defVal) {
                    depart.clearItems();
                } else {
                    depart.param.fatherid = this.getValue();
                    depart.reload();
                }
            }
        });
		
		var depart=new Chenxin.UI.StructCombo({
            autoLoad: false,
            param: { op: 'getdeparts' },
            name: 'depid',
            txtModelName: 'dep',
            colWidth: 120,  
			inModelType:'all',
            containerId: '#' + this.id + '_Depart',
            tbxWidth: 130
        });
		return [city,company,depart];
	},
	initComponent:function(){
		Chenxin.UI.BusinessSetting.superclass.initComponent.call(this);
	}
});

Chenxin.UI.DefAuthoritySetting=Chenxin.extend(Chenxin.UI.FormCommon,{
	actionUrl:'service/OrganizationMgr.ashx',
	id:'defauthoritysettingWin',
	renderTo:'jqm_defauthoritysetting',
	createForm:function(){
		return Chenxin.FormCommon.defAuthoritySetting(this.id);
	},
	initEvent:function(){	
		var _this=this;
		var filter='#'+this.id;
		$(filter+'_Save').live('click',function(){			
			_this.save();
		});
		$(filter+'_Cancle').live('click',function(){
			_this.hide();			
		});	
		
		Chenxin.Fn.createCheckBoxGroup(comboCommon.purview,'purview',130,'td_purview');
	},
	loadPurview:function(depid,terms){	
		var _this=this;
		Chenxin.ajax({
			url:this.actionUrl+'?op=gettermssetting',
			type:'get',
			data:{departid:depid,terms:terms},
			onSuccess:function(data,msg){
				_this.fitData(data,true);
			}
		});
	},
	createComboList:function(){
		var _this=this;
        //职位
        var authority = new Chenxin.UI.Combo({
            name: 'terms',
            containerId: '#' + this.id + '_Authority',
			items:comboCommon.authority,
			inModelType:'txt',
            tbxWidth: 130
        });
	
		//城市
        var city = new Chenxin.UI.StructCombo({
            param: { op: 'getcity' },
            containerId: '#' + this.id + '_City',
            tbxWidth: 130,
            inModelType: 'none',
            onSelected: function() {
                if (this.getValue() == this.defVal) {
                    company.clearItems();
                } else {
                    company.param.fatherid = this.getValue();
                    company.reload();
                }
            }
        });

        //公司
        var company = new Chenxin.UI.StructCombo({
            autoLoad: false,
            param: { op: 'getcompany' },
			name:'departid',
            containerId: '#' + this.id + '_Company',
            tbxWidth: 130,
			required:true,
			onSelected:function(){
				if(this.getValue()==this.defVal){
					depart.clearItems();	
				}else{
					depart.param.fatherid=this.getValue();
					depart.reload();
				}
				if(this.getValue()!=this.defVal&&authority.getValue()!=authority.defVal){
					_this.loadPurview.call(_this,this.getValue(),authority.getText());
				}
			}
        });
		
		var depart=new Chenxin.UI.StructCombo({
            autoLoad: false,
            param: { op: 'getdeparts' },
            name: 'departid',
            containerId: '#' + this.id + '_Depart',
            tbxWidth: 130,
            colNum: 3,
            hasPage: true,
            pageSize: 15,
            hasSearch: true,
			onSelected:function(){
				if(this.getValue()!=this.defVal&&authority.getValue!=authority.defVal){
					_this.loadPurview.call(_this,this.getValue(),authority.getText());
				}
			}
        });
		return [city,company,depart,authority];
	},
	initComponent:function(){
		Chenxin.UI.DefAuthoritySetting.superclass.initComponent.call(this);
	}
});

Chenxin.UI.MoveNodeForm=Chenxin.extend(Chenxin.UI.FormCommon,{
	actionUrl:'service/OrganizationMgr.ashx',
	id:'moveNodeWin',
	renderTo:'jqm_moveNode',
	jqmWidth:400,
	createForm:function(){
		return Chenxin.FormCommon.moveNodeForm(this.id);
	},
	initEvent:function(){	
		var _this=this;
		var filter='#'+this.id;
		$(filter+'_Save').live('click',function(){			
			_this.save();
		});
		$(filter+'_Cancle').live('click',function(){
			_this.hide();			
		});	
		
	},
	createComboList:function(){
		var _this=this;
	
		//城市
        var city = new Chenxin.UI.StructCombo({
            param: { op: 'getcity' },
            containerId: '#' + this.id + '_City',
            tbxWidth: 130,
            inModelType: 'none',
            onSelected: function() {
                if (this.getValue() == this.defVal) {
                    company.clearItems();
                } else {
                    company.param.fatherid = this.getValue();
                    company.reload();
                }
            }
        });

        //公司
        var company = new Chenxin.UI.StructCombo({
            autoLoad: false,
            param: { op: 'getcompany' },
			inModelType:'none',
            containerId: '#' + this.id + '_Company',
            tbxWidth: 130,
			onSelected:function(){
				if(this.getValue()==this.defVal){
					depart.clearItems();	
				}else{
					depart.param.fatherid=this.getValue();
					depart.reload();
				}
			}
        });
		//部门
		var depart=new Chenxin.UI.StructCombo({
            autoLoad: false,
            param: { op: 'getbusinessdistrict' },
            name: 'parentid',
            containerId: '#' + this.id + '_Depart',
            tbxWidth: 130,
			required:true
        });
		return [city,company,depart];
	},
	initComponent:function(){
		Chenxin.UI.MoveNodeForm.superclass.initComponent.call(this);
	}
});

Chenxin.FormCommon.organizationForm=function (comboIdPrefix){
return '<table class="tbl_f"><tbody><tr><td colspan="4"><b>基本信息</b></td></tr><tr><th style="width:100px"><span class="isfilled">*</span><span id="lbl_departname">城市名称</span>：</th><td><input id="departname" name="DepartName" class="inputbox_n fl" type="text"/><div id="departnameTip" style="float:left; margin-left: 5px; width: 120px; height: 26px; line-height: 26px; text-align: left;"></div></td></tr><tr><th><span class="isfilled">*</span><span id="lbl_departcode">城市编号</span>：</th><td><input id="departcode" name="DepartCode" class="inputbox_n fl" type="text"/><div id="departcodeTip" style="float:left; margin-left: 5px; width: 120px; height: 26px; line-height: 26px; text-align: left;"></div></td></tr><tr><th>开通状态：</th><td><input type="radio" value="true" id="chkIsOpen" name="IsOpen" />开通<input type="radio" value="false" id="chkIsOpen1" name="IsOpen" />关闭</td></tr><tr><th>计算业绩：</th><td><input type="radio" value="true" name="IsCalculate" />是<input type="radio" value="false" name="IsCalculate" />否</td></tr><tr id="tr_areasetting" style="display:none"><th style="border-top:dashed 1px #ccc;">片区规则适用：</th><td style="border-top:dashed 1px #ccc;"><input type="radio" value="true" id="radio_area_yes" name="AreaUse" /><label for="radio_area_yes">适用</label><input type="radio" value="false" id="radio_area_no" name="AreaUse" /><label for="radio_area_no">不适用</label></td></tr><tr id="tr_areatype" ><th>跨片区房源：</th><td><input type="radio" value="10" id="radio_areatype_10" name="AreaType" /><label for="radio_areatype_10">不显示</label><input type="radio" value="20" id="radio_areatype_20" name="AreaType" /><label for="radio_areatype_20">显示模糊信息，不能查询业主</label><input type="radio" value="30" id="radio_areatype_30" name="AreaType" /><label for="radio_areatype_30">显示详细信息，不能查询业主</label></td></tr><tr id="tr-moreitems" style="display:none"><td colspan="2"><table> <tr><th style="width:100px"><span class="isfilled">*</span>部门类别：</th><td><div id="' + comboIdPrefix + '_DepartKind" class="SelectWrapSingle"></div></td><th style="width:100px"><span class="isfilled">*</span>所在区域：</th><td><div id="' + comboIdPrefix + '_Region" class="SelectWrapSingle" style="z-index:500;margin-right: 5px;"></div></td></tr><tr><th>部门电话：</th><td><input name="Tel" class="inputbox_n fl" type="text" style="width:125px;" maxlength="50"/></td><th style="width:70px;">Email地址：</th><td><input name="Email" class="inputbox_n fl" type="text" style="width:125px;" maxlength="50"/>  </td>  </tr><tr><th style="width:70px;">部门地址：</th><td colspan="3"><input name="Address" class="inputbox_n fl" type="text" style="width:463px;" maxlength="50"/></td></tr>  <tr><th>商圈平台：</th><td><input type="checkbox" value="true" name="IsRoof" />是</td><th>签约中心：</th><td><input type="checkbox" value="true" name="IsContractCenter" />是</td></tr><tr><th>管理区域：</th><td colspan= "3" ><div id="' + comboIdPrefix + '_RegionCtr" class="SelectWrapSingle" style="z-index:400;margin-right: 5px;"></div></td></tr><tr><td colspan="4"><div class="tdline"></div><b>参数设定</b><br /></td></tr><tr class="tdheight"> <td colspan="2"><span class="magsidlf">每天查看电话数量限制</span><input type="text" id="txtOwnerNum" name="ViewOwnerNum" style="width: 50px;" class="inputbox_n" />(次)&nbsp;&nbsp; </td><td colspan="2" >封盘最长有效期<input type="text" id="txtLockHouseDay" name="LockHouseDay" class="inputbox_n"  style="width: 50px;" />(天) </td></tr><tr  class="tdheight"><td colspan="2"><span class="magsidlf">手动保护房源设定</span><input type="text" id="txtProtectedNum" name="ProtectedNum" style="width: 50px;"  class="inputbox_n" />(套)&nbsp;&nbsp;</td><td colspan="2">保护时限<input type="text" id="txtMarkProtectHours" name="MarkProtectHours" style="width: 50px;"  class="inputbox_n" />(小时)</td></tr><tr class="tdheight"><td colspan="2">需求客户数量上限<input type="text" id="txtCustomerDemandNum" name="CustomerDemandNum" style="width: 50px;" class="inputbox_n"/>(个)(0为不限)&nbsp;&nbsp; </td><td colspan="2">超时未客户/客需跟单上限<input type="text" id="txtOverTimeTraceNum" name="OverTimeTraceNum" style="width: 50px;" class="inputbox_n"/>(天)(0为不限)</td></tr><tr class="tdheight"><td colspan="4"><div class="magsidlf fl"><span class="isfilled">*</span>可添加房源的所在城市：</div><div id="' + comboIdPrefix + '_canaddcity" class="SelectWrapSingle fl" style="z-index:395;margin-right: 5px;"></div></td></tr><tr class="tdheight"><td colspan="4">  <div class="magsidlf"> <span class="spanwh138"><input type="checkbox" value="true" id="cbxHdeHouse" name="IsHideHouse" />房源是否隐藏</span><span class="spanwh138"><input type="checkbox" value="true" id="cbxIsProtectMark" name="IsProtectMark" />备案进行保护</span> <span class="spanwh138"><input type="checkbox" value="true" id="cbxAllowPrivHouse" name="AllowPrivHouse" />允许输入私盘  </span><span class="spanwh138"><input type="checkbox" value="true" id="cbxAllowPrivDemand" name="AllowPubDemand" />允许输入公需</span>  </div></td></tr><tr><td colspan="4"><div class="magsidlf"><span class="spanwh138"><input type="checkbox" value="true" id="cbxIsUseBlacklist" name="IsUseBlacklist" />使用黑名单过滤 </span><span class="spanwh150"><input type="checkbox" id="cbxIsTelRepeat" name="IsTelRepeat" value="true"/>入客需，电话号码重复性检查  </span></div></td></tr><tr class="tdheight"><td colspan="4"><div class="magsidlf">黑名单级别：<input type="radio" id="rdoCompany" name="BlacklistLevel" value="10" checked="checked" />10:公司级；<input type="radio" id="rdoDePart" name="BlacklistLevel" value="20" />20:部门级；<input type="radio" id="rdoPerson" name="BlacklistLevel" value="30" />30:个人级；</div></td></tr>  <tr></table></td></tr></tbody></table>';
};

Chenxin.FormCommon.businessSetting=function(comboIdPrefix){
	return '<table class="tbl_f"><tr><th style="width:80px;">工号：</th><td><input id="'+comboIdPrefix+'_usercode" name="usercode" class="inputbox_n fl" type="text"  style="width:160px;margin-left: 5px;" /><div id="'+comboIdPrefix+'_usercodeTip" style="float: left; margin-left: 5px; width: 90; height: 26px; line-height: 26px; text-align: left;" class="fl"></div></td></tr><tr><th></th><td><input name="UserName" class="inputbox_n fl" style="width:160px;margin-left: 5px;" readonly="readonly"  /></td></tr><tr><th>城市：</th><td><div id="'+comboIdPrefix+'_City" class="SelectWrapSingle" style="z-index:505; margin-left:5px;"></div></td></tr><tr><th>公司：</th><td><div id="'+comboIdPrefix+'_Company" class="SelectWrapSingle" style="z-index:504; margin-left:5px;"></div></td></tr><tr><th>商圈：</th><td><div id="'+comboIdPrefix+'_BusinessDistinct" class="SelectWrapSingle" style="z-index:503; margin-left:5px;"></div></td></tr><tr><th>部门：</th><td><div id="'+comboIdPrefix+'_Depart" class="SelectWrapSingle" style="z-index:502; margin-left:5px;"></div></td></tr><tr><td colspan="2" align="right"><div style="float:right; margin-right:15px;"><a id="' + comboIdPrefix + '_Save" class="btn_n fl" href="#"><span>确定</span></a><a id="' + comboIdPrefix + '_Cancle"  class="btn_n fl" href="#"><span>取消</span></a></div></td></tr></table>';
};


Chenxin.FormCommon.moveNodeForm=function(comboIdPrefix){
	return '<table class="tbl_f"><tr><th>目标城市：</th><td><div id="'+comboIdPrefix+'_City" class="SelectWrapSingle" style="z-index:505; margin-left:5px;"></div></td></tr><tr><th>目标公司：</th><td><div id="'+comboIdPrefix+'_Company" class="SelectWrapSingle" style="z-index:504; margin-left:5px;"></div></td></tr><tr><th>目标商圈(分类)：</th><td><div id="'+comboIdPrefix+'_Depart" class="SelectWrapSingle" style="z-index:503; margin-left:5px;"></div></td></tr><tr><td colspan="2" align="right"><div style="float:right; margin-right:15px;"><a id="' + comboIdPrefix + '_Save" class="btn_n fl" href="#"><span>确定</span></a><a id="' + comboIdPrefix + '_Cancle"  class="btn_n fl" href="#"><span>取消</span></a></div></td></tr></table>';

};

Chenxin.FormCommon.defAuthoritySetting=function(comboIdPrefix){
	return '<table class="tbl_f"><tr><th style="width:80px;">职位：</th><td><div id="'+comboIdPrefix+'_Authority" class="SelectWrapSingle" style="z-index:506; margin-left:5px;"></div></td></tr><tr><th>城市：</th><td><div id="'+comboIdPrefix+'_City" class="SelectWrapSingle" style="z-index:505; margin-left:5px;"></div></td></tr><tr><th>公司：</th><td><div id="'+comboIdPrefix+'_Company" class="SelectWrapSingle" style="z-index:504; margin-left:5px;"></div></td></tr><tr><th>部门：</th><td><div id="'+comboIdPrefix+'_Depart" class="SelectWrapSingle" style="z-index:503; margin-left:5px;"></div></td></tr><tr><td colspan="2"><div class="tdline"></div></td></tr><tr><th>业务功能：</th><td id="td_purview"></td></tr><tr><td colspan="2" align="right"><div style="float:right; margin-right:15px;"><a id="' + comboIdPrefix + '_Save" class="btn_n fl" href="#"><span>确定</span></a><a id="' + comboIdPrefix + '_Cancle"  class="btn_n fl" href="#"><span>取消</span></a></div></td></tr></table>';
};
$(document).ready(function(){
	var ogm=new Chenxin.UI.OrganizationMgr();
});

customerExpand=function(){
	$('#td_table').css('width','616px');
	$('#emplist_table').css('width','616px');
};
customerCollapse=function(){
	$('#td_table').css('width','766px');
	$('#emplist_table').css('width','766px');	
};